# Set directories
basedir <- "G:/Meu Drive/fish_mgmt_areas/data"
indir <- file.path(basedir, "brazil", "raw")
outdir <- file.path(basedir, "brazil", "processed")
plotdir <- "figures"
# read in the data
df_orig <- st_read(file.path(indir, "ZonaCosteiraMarinha_FINAL.shp")) %>% 
  clean_names()
## Reading layer `ZonaCosteiraMarinha_FINAL' from data source `G:\Meu Drive\fish_mgmt_areas\data\brazil\raw\ZonaCosteiraMarinha_FINAL.shp' using driver `ESRI Shapefile'
## Simple feature collection with 116 features and 13 fields
## geometry type:  MULTIPOLYGON
## dimension:      XYZ
## bbox:           xmin: -53.36983 ymin: -37.29693 xmax: -29.04385 ymax: 8.777154
## z_range:        zmin: 0 zmax: 0
## geographic CRS: SIRGAS 2000
# rnaturalearth data
world <- ne_countries(scale = "medium", returnclass = "sf")
# Set coordinate system
wgs84 <- st_crs("+proj=longlat +datum=WGS84")
# Inspect attribute table
df <- df_orig %>% 
  st_drop_geometry()
# Wrangle the data into a df and export it
# df_final <- df_orig %>% 
#   mutate(dataset = "GFCM Statistical Areas",
#          country = "Multi-national",
#          ocean = "Mediterranean",
#          region = "Mediterranean",
#          agency = "General Fisheries Commission for the Mediterranean",
#          zone_name = NA,
#          species = "multi-species",
#          source = "https://www.fao.org/gfcm/data/maps/gsas") %>% 
#   rename(zone_id = f_gsa_lib) %>% 
#   select(dataset, country, ocean, region, agency, zone_id, zone_name, species, source) %>% 
#   st_transform(wgs84)
# Create output RDS file
#saveRDS(df_final, file = file.path(outdir, "GSAs_simplified.Rds"))
  #    +     geom_sf(data = world),
          #aes(color = name)) +
# Create plot
data("World")
tmap_mode("view")
## tmap mode set to interactive viewing
bb_brazil <- bb("Brazil", projection = "wgs84")
tm_shape(World, bbox = bb_brazil) +
  tm_polygons("iso_a3", legend.show = FALSE) +
  tm_shape(df_orig) +
    tm_polygons("ac_princ_nom", legend.show = FALSE) +

ggsave(file.path(outdir, "Brazil_priority_areas_elasmos_coastal_zone.png"), width = 8, height = 8)
## Warning: Number of levels of the variable "iso_a3" is 177, which is
## larger than max.categories (which is 30), so levels are combined. Set
## tmap_options(max.categories = 177) in the layer function to show all levels.